package com.sharemarking.application.common.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sharemarking.application.common.dto.UserDetailImpl;
import com.sharemarking.application.common.entity.User;
import com.sharemarking.application.common.repository.UserRepo;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
	
	@Value("${project.session.loginValidateCode}")
	String valiKey;
	
	@Autowired
	UserRepo repo;
	
	@Autowired  
	private HttpSession session;
	
	@Autowired
	private HttpServletRequest request;

	@Override
	@Transactional
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		String code=(String)session.getAttribute(valiKey);
		String valiCodeParam=request.getParameter("loginValidateCode");
		/*if(code==null || !code.equalsIgnoreCase(valiCodeParam)){
			throw new UsernameNotFoundException("验证码错误");
		}*/
		User member = repo.findByLoginName(username);
		if(member==null||Boolean.TRUE.equals(member.getDelFlag()))throw new UsernameNotFoundException("用户名错误");
		return new UserDetailImpl(member);
	}

}
